package com.xx.ems.model;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import com.xx.ems.util.impl.ObjectTransformImpl;

/**
 * @author xxx
 * @date 2014-2-25
 * @version 1.0
 * 描述: 用户表
 */
@Entity
public class Users {
	/**
	 * 描述：主键
	 */
	private Integer uid;
	/**
	 * 描述：用户名
	 */
	private String username;
	/**
	 * 描述：账号
	 */
	private String account;
	/**
	 * 描述：密码
	 */
	private String password;
	/**
	 * 描述：性别 (默认值：male(男))
	 */
	private String sex;
	/**
	 * 描述：邮箱
	 */
	private String email;
	/**
	 * 描述：手机号码
	 */
	private String phone;
	/**
	 * 描述：头像URL (默认值：head-image-default.png)
	 */
	private String head_portrait;
	/**
	 * 描述：部门表关联对象
	 */
	private Departments department;
	/**
	 * 描述：部门表关联外键
	 */
	private Integer fk_did;
	/**
	 * 描述：角色表关联对象
	 */
	private Set<Roles> roles = new HashSet<Roles>();
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="uid",unique=true,nullable=false)
	public Integer getUid() {
		return uid;
	}
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	@Column(name="username",length=20,nullable=false)
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	@Column(name="account",length=18,nullable=false)
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	@Column(name="password",length=32,nullable=false)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Column(name="sex",length=6,nullable=false)
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Column(name="email",length=50,nullable=false)
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Column(name="phone",length=18,nullable=false)
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Column(name="head_portrait",length=100,nullable=false)
	public String getHead_portrait() {
		return head_portrait;
	}
	public void setHead_portrait(String head_portrait) {
		this.head_portrait = head_portrait;
	}
	@ManyToOne(targetEntity=Departments.class)
	@JoinColumn(name="fk_did",insertable=false,updatable=false)
	public Departments getDepartment() {
		return department;
	}
	public void setDepartment(Departments department) {
		this.department = department;
	}
	@Column(name="fk_did")
	public Integer getFk_did() {
		return fk_did;
	}
	public void setFk_did(Integer fk_did) {
		this.fk_did = fk_did;
	}
	@ManyToMany(targetEntity=Roles.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	@JoinTable(name="users_roles",
		joinColumns={@JoinColumn(name="fk_uid")},
		inverseJoinColumns={@JoinColumn(name="fk_rid")})
	public Set<Roles> getRoles() {
		return roles;
	}
	public void setRoles(Set<Roles> roles) {
		this.roles = roles;
	}
	/**
	 * 描述: 将实体的属性及相应的值转换成Map类型(为了方便转换成json格式后前台调用) 
	 * @return
	 * @date 2014-3-3
	 */
	public Map<String, Object> toHashMap() {
		Map<String, Object> map = new ObjectTransformImpl().javabeanToMap(this);
		map.put("department", getDepartment().getDname());
		map.remove("roles");
		return map;
	}
}